package cloud.maque.module.system.service.impl;

import cloud.maque.common.core.exceptions.BusinessException;
import cloud.maque.common.core.request.PageRequest;
import cloud.maque.common.core.utils.TokenUserUtil;
import cloud.maque.common.utils.PageRequestUtil;
import cloud.maque.module.system.entity.SysPost;
import cloud.maque.module.system.mapper.PostMapper;
import cloud.maque.module.system.service.PostService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;

@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, SysPost> implements PostService {


    @Autowired
    PageRequestUtil util;

    @Autowired
    TokenUserUtil userUtil;

    @Override
    public IPage<SysPost> GetList(PageRequest<SysPost> pageReq) {
        Page<SysPost> page = util.GetPage(SysPost.class, pageReq);
        QueryWrapper<SysPost> query = new QueryWrapper<>();
        return this.page(page, query);
    }

    @Override
    public SysPost SavePost(SysPost data) {

        Assert.hasText(data.getPostName(), "岗位名称不能为空");
        Assert.hasText(data.getPostCode(), "岗位代码不能为空");

        if (StrUtil.isEmptyIfStr(data.getPostId())) {
            data.setCreateBy(userUtil.GetUserName());
            data.setCreateTime(new Date());
            data.setUpdateBy(data.getCreateBy());
            data.setUpdateTime(new Date());
        } else {
            SysPost oldPost = this.baseMapper.selectById(data.getPostId());
            if (oldPost != null) {
                data.setCreateTime(oldPost.getCreateTime());
                data.setCreateBy(oldPost.getCreateBy());
            }
            data.setUpdateBy(userUtil.GetUserName());
            data.setUpdateTime(new Date());

        }

        SysPost oldPost = GetByPostName(data.getPostName());
        if (oldPost != null && !oldPost.getPostId().equals(data.getPostId())) {
            if (oldPost.getPostName().equalsIgnoreCase(data.getPostName())) {
                throw new BusinessException("岗位名称已存在");
            }
        }
        oldPost = GetByPostCode(data.getPostName());
        if (oldPost != null && !oldPost.getPostId().equals(data.getPostId())) {

            if (oldPost.getPostCode().equalsIgnoreCase(data.getPostCode())) {
                throw new BusinessException("岗位代码已存在");
            }
        }

        this.saveOrUpdate(data);
        return data;
    }

    private SysPost GetByPostName(String postName) {

        QueryWrapper<SysPost> query = new QueryWrapper<>();
        query.lambda().eq(SysPost::getPostName, postName);
        return this.getOne(query, false);
    }

    private SysPost GetByPostCode(String postCode) {

        QueryWrapper<SysPost> query = new QueryWrapper<>();
        query.lambda().eq(SysPost::getPostCode, postCode);
        return this.getOne(query, false);
    }

    /**
     * 修改岗位状态
     *
     * @param data
     * @return
     */
    @Override
    public SysPost EditStatus(SysPost data) {
        Assert.hasText(data.getPostId(), "ID不能为空");

        SysPost post = this.getById(data.getPostId());
        if (post == null) {
            throw new BusinessException("岗位不存在");
        }
        if ("1".equals(post.getStatus())) {
            post.setStatus("0");
        } else {
            post.setStatus("1");
        }
        UpdateWrapper<SysPost> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(SysPost::getPostId, post.getPostId());
        updateWrapper.lambda().set(SysPost::getStatus, post.getStatus());
        updateWrapper.lambda().set(SysPost::getUpdateBy, userUtil.GetUserName());
        updateWrapper.lambda().set(SysPost::getUpdateTime, new Date());
        this.update(updateWrapper);
        return post;
    }

    @Override
    public Boolean DeletePost(String postId) {
        return this.removeById(postId);
    }
}
